package com.demo.order.service.impl;


import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.demo.order.bean.Order;
import com.demo.order.feign.ProductFeignClient;
import com.demo.order.service.OrderService;
import com.demo.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
@Slf4j
@Service
@GlobalTransactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;//一定要导入spring-cloud-starter-loadbalancer
    @Autowired
    private ProductFeignClient productFeignClient;
    @Override
    @SentinelResource(value = "createOrder",blockHandler = "createOrderFallback")
    public Order createOrder(Long productId, Long userId) {
//        Product product = getProductFromRemoteWithLoadBalanceAnnotation(productId);
        //使用Feign
        Product product = productFeignClient.getProductById(productId, "");
        Order order = new Order();
        order.setId(1L);

        order.setAddress("whpu");
        order.setNickName("zhangsan");
        order.setUserId(userId);
        // 总金额


        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        //远程查询
        order.setProductList(Arrays.asList(product));

        return order;
    }

    //兜底回调，方法签名要一样，名称为blockHandler所指
    public Order createOrderFallback(Long productId, Long userId,BlockException e) {
        Order order=new Order();
        order.setId(0L);
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("未知用户");
        order.setAddress("异常信息"+e.getMessage());
        return order;

    }
    private Product getProductFromRemote(Long productId){
        //获取商品服务所在ip和port
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        String url = "http://"+instance.getHost() + ":" + instance.getPort()+"/product/"+productId;
        log.info("远程请求:{}",url);
        //给远程发送请求
//        RestTemplate restTemplate = new RestTemplate();
        Product product = restTemplate.getForObject(url, Product.class);
        //负载均衡



        return product;

    }
    //负载均衡
    private Product getProductFromRemoteWithLoadBalance(Long productId){
        //获取商品服务所在ip和port
        ServiceInstance instance = loadBalancerClient.choose("service-product");
        String url = "http://"+instance.getHost() + ":" + instance.getPort()+"/product/"+productId;
        log.info("远程请求:{}",url);
        //给远程发送请求
//        RestTemplate restTemplate = new RestTemplate();
        Product product = restTemplate.getForObject(url, Product.class);
        //负载均衡



        return product;

    }

    //注解版负载均衡
    private Product getProductFromRemoteWithLoadBalanceAnnotation(Long productId){
        String url = "http://service-product/product/"+productId;
        log.info("远程请求:{}",url);
        //给远程发送请求.动态替换service-product
//        RestTemplate restTemplate = new RestTemplate();
        Product product = restTemplate.getForObject(url, Product.class);
        //负载均衡



        return product;

    }
}
